<?php
/**
 * @Author: rocky
 * Date: 2019/9/27
 * Time: 10:25
 */

namespace app\shop\controller\api\v1;

use app\common\controller\BaseApiController;
use app\common\facade\Token;
use app\common\model\ShopStore;
use app\common\model\ShopMessageTip;
use app\common\service\LatLngTranslate;
use app\common\tools\Arr;
use app\common\tools\Files;
use app\shop\model\ShopBanner;
use app\common\model\ShopSchoolRegion;
use app\shop\model\ShopOrder;
use app\shop\resource\StoreResoure;
use app\shop\model\ShopGoodsCate;
use app\common\model\ShopMember;
use think\Db;

class Index extends BaseApiController
{
//    轮播图banner
    public function getBanner()
    {
        $data = ShopBanner::field('img_url,page_url')->where('status',1)->all();
        $this->successCode($data);
    }
    //判断会员卡是否准备到期
    public function getIsMember()
    {
        $uid = $this->request->get('uid');
        $data = ShopMember::where('uid',$uid)->find();
        $now = time();
        $future = $data['expire_time'];
        $diff = $future - $now;
        $days = ceil($diff / 86400);
        $this->successCode($days);
    }
    
    //首页分类
     public function getIndexCate()
    {
        $pid = $this->request->get('pid');
        $data = ShopGoodsCate::when($pid, function ($q) use ($pid) {
            $q->where('pid', $pid);
        }, function ($q) {
            $q->where('pid', 0);
        })->where('status', 1)->field('id,title,img,node')->where('is_deleted', 0)->select();
        $this->successCode($data);
    }

    /*
     * 区域信息
     */
    public function getRegion()
    {
        $region = ShopSchoolRegion::field('id,title as text,pid')->select()->toArray();
        $data = Arr::tree($region,0);
        $this->successCode($data);
    }
    
    //根据用户定位门店
    public function getSchool()
    {
        $lng = $this->request->get('lng');
        $lat = $this->request->param('lat');
        $site = ShopStore::where('status',1)->field('id,name,longitude,latitude,city,area,address')->select();
        if ($site){
            $address = [];
            foreach ($site as $key => $value){
                $distance = LatLngTranslate::GetDistance($lat,$lng,$value['latitude'],$value['longitude']);
                $address[] = [
                    'id' => $value['id'],
                    'distance' => $distance,
                    'name' => $value['name'],
                    'full_address' => $value['full_address'],
                    'lat' => $value['latitude'],
                    'lng' => $value['longitude']
                ];
            }
        }
        // 根据计算出的距离进行排序
        array_multisort(array_column($address,'distance'),SORT_ASC,$address);
        if ($address) {
             if($address[0]['distance'] > 10){
                    $this->errorCode(100,'附近3公里无门店');
                }

             //用户绑定和更新门店id
            Db::name('user')->where('id', Token::id())->update(['original_store_id' => $address[0]['id'],'store_id' => $address[0]['id']]);
            $this->successCode($address[0]);
        }
    }
    
    /*
     * 店铺信息
     */
    public function getStoreInfo()
    {
        $sid = $this->request->get('sid');
        $data = ShopStore::where('status',1)->where('id',$sid)->find();
        $this->successCode($data);
    }
    //门店地图信息
    public function getMapStore()
    {
        $lat = $this->request->get('lat');
        $lng = $this->request->get('lng');
        
        $data = ShopStore::select();
        
        if ($data){
            foreach ($data as $key => $value){
                $distance = LatLngTranslate::GetDistance($lat,$lng,$value['latitude'],$value['longitude']);
                $data[$key]['distance'] = $distance;
            }
        }
        $this->successCode($data);
    }
    
    //关于我们
    public function getAbout(){
        $data['customer_tel'] = sysconf('customer_tel');
        $data['customer_email'] = sysconf('customer_email');
        $data['work_time'] = sysconf('work_time');
        $data['mini_version'] = sysconf('mini_version');
        $data['mini_logo'] = Files::getImgUrl(sysconf('mini_logo'));
        $data['mini_name'] = sysconf('mini_name');
        $data['about_us_text'] = htmlspecialchars_decode(sysconf('about_us_text'));
        $this->successCode($data);
    }
    
    //客服热线
    public function getCustomer(){
        $data['customer_tel'] = sysconf('customer_tel');
        $this->successCode($data);
    }

    //用户协议
    public function getUserText(){
        $data['text'] = htmlspecialchars_decode(sysconf('user_sale_text'));
        $this->successCode($data);
    }
    
    //消息、订单通知
    public function getNews(){
        $data['num'] = ShopMessageTip::uid()->where('status',0)->count();
        $underway_order_id = ShopOrder::uid()->where('status',3)->find()['id'];
        $data['underway_order_id'] = $underway_order_id?$underway_order_id:0;
        $end_order_id = ShopOrder::uid()->where('status',4)->find()['id'];
        $data['end_order_id'] = $end_order_id?$end_order_id:0;
        $this->successCode($data);
    }

    //最近订单二维码
    public function getOrderQrCode()
    {
        $uid = Token::id();
        $this->successCode($uid);

    }

}